package com.oocl.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oocl.dao.impl.BookOrderDaoImpl;
import com.oocl.pojo.GoodOrder;
import com.oocl.pojo.GoodsInfo;

/**
 * Servlet implementation class BuyServlet
 */
public class BuyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private List<GoodsInfo> goodsInfoList;   
	private BookOrderDaoImpl bookOrderDaoImpl = new BookOrderDaoImpl();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BuyServlet() {
        super();
        goodsInfoList = new ArrayList<GoodsInfo>();
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		goodsInfoList = (List<GoodsInfo>)request.getSession().getAttribute("goodsInfoList");
		if(goodsInfoList == null)
			goodsInfoList = new ArrayList<GoodsInfo>();
		for(int i = 0; i < goodsInfoList.size(); i++){
			String id = goodsInfoList.get(i).getId();
			String uid = goodsInfoList.get(i).getUid();
			String bookName = goodsInfoList.get(i).getBookName();
			int count = goodsInfoList.get(i).getCount();
			
			GoodOrder goodOrder = new GoodOrder(id,uid,bookName,count);
			bookOrderDaoImpl.addOrder(goodOrder);
		}
		
		request.getRequestDispatcher("buy.jsp").forward(request, response);
	}

}
